import Vue from 'vue';
import VueRouter from 'vue-router';
import Main from '../views/Main';
import CategoryEdit from '../views/CategoryEdit';
import CategoryList from '../views/CategoryList';
import ItemsEdit from '../views/ItemsEdit';
import ItemsList from '../views/ItemsList';
import HeroesEdit from '../views/HeroesEdit';
import HeroesList from '../views/HeroesList';
import ArticlesEdit from '../views/ArticlesEdit';
import ArticlesList from '../views/ArticlesList';
import AdsEdit from '../views/AdsEdit';
import AdsList from '../views/AdsList';
import AdminUserEdit from '../views/AdminUserEdit';
import AdminUserList from '../views/AdminUserList';
import Login from '../views/Login';

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { isPublic: true },
  },
  {
    path: '/',
    name: 'Main',
    component: Main,
    children: [
      { path: '/categories/create', component: CategoryEdit },
      { path: '/categories/edit/:id', component: CategoryEdit, props: true },
      { path: '/categories/list', component: CategoryList },

      { path: '/items/create', component: ItemsEdit },
      { path: '/items/edit/:id', component: ItemsEdit, props: true },
      { path: '/items/list', component: ItemsList },

      { path: '/heroes/create', component: HeroesEdit },
      { path: '/heroes/edit/:id', component: HeroesEdit, props: true },
      { path: '/heroes/list', component: HeroesList },

      { path: '/articles/create', component: ArticlesEdit },
      { path: '/articles/edit/:id', component: ArticlesEdit, props: true },
      { path: '/articles/list', component: ArticlesList },

      { path: '/ads/create', component: AdsEdit },
      { path: '/ads/edit/:id', component: AdsEdit, props: true },
      { path: '/ads/list', component: AdsList },

      { path: '/adminUser/create', component: AdminUserEdit },
      { path: '/adminUser/edit/:id', component: AdminUserEdit, props: true },
      { path: '/adminUser/list', component: AdminUserList },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (!sessionStorage.token && !to.meta.isPublic) {
    return next('/login');
  }
  next();
});

export default router;
